﻿using HuaweiCloud.Dotnet.Samples.Common;
using HuaweiCloud.Dotnet.Samples.Entities;
using Microsoft.AspNetCore.Mvc;


namespace HuaweiCloud.Dotnet.Samples.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class WeatherController : ControllerBase
    {
        private readonly IRepository<Weather> _repository;
        public WeatherController(IRepository<Weather> repository)
        {
            _repository = repository;
        }
        // GET: api/<WeatherController>
        [HttpGet]
        public async Task<IEnumerable<Weather>> Get()
        {
            return await _repository.ListAsync();
        }

        // GET api/<WeatherController>/5
        [HttpGet("{id}")]
        public async Task<Weather?> Get(int id)
        {
            return await _repository.GetByIdAsync(id);
        }

        // POST api/<WeatherController>
        [HttpPost]
        public async Task Post([FromBody] Weather value)
        {
            await _repository.AddAsync(value);
        }

        // PUT api/<WeatherController>/5
        [HttpPut("{id}")]
        public async Task Put(int id, [FromBody] Weather value)
        {
            value.Id = id;
            await _repository.UpdateAsync(value);
        }

        // DELETE api/<WeatherController>/5
        [HttpDelete("{id}")]
        public async Task Delete(int id)
        {
            var item = await _repository.GetByIdAsync(id);
            if (item != null)
                await _repository.DeleteAsync(item);
        }
    }
}
